package com.draw.server.bdd_manager;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.draw.core.Word;
import com.draw.server.hibernate.HibernateUtil;
/**
 * Cette classe fourni les méthode permettant au serveur de communiquer avec la base de données
 * pour la gestions des mots.
 * 
 * @author Yann et Damien
 *
 */
public class WordManager {
	public static void createWord(Word word) {	
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		session.save(word);
		tx.commit();
	}
	
	public static void updateWord(Word word) {	
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		session.merge(word);
		tx.commit();
	}
	
	public static void deleteWord(Word word) {
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		session.delete(word);
		tx.commit();
	}
	
	public static int countWordByLevel(int level)
	{
		Session session = HibernateUtil.getSession();
		Long res =( (Long) session.createQuery("select count(*) from Word where level="+level).iterate().next() );
		//System.out.println("\nNB mot pour le niveau "+level+" : "+res.intValue());
		return 	res.intValue();
	}
	
	public static int countWord()
	{
		Session session = HibernateUtil.getSession();
		Long res =( (Long) session.createQuery("select count(*) from Word").iterate().next() );
		//System.out.println("\nNB mot total: "+res.intValue());
		return 	res.intValue();
	}
}
